import O1HD, { services } from "../engines/gameEngine";
import data_control from "../engines/libs/data_control";
import ServicesBase from "./Base/ServicesBase";
import SystemBase from "./Base/SystemBase";
import { GAME_EVENT, PAGE } from "./Enum";
import ConfigService from "./Services/ConfigService";
import InputSystem from "./System/InputSystem";
import LevelSystem from "./System/LevelSystem";
import UISystem from "./System/UISystem";

class MainRoot {
    private uiRoot: cc.Node = null;

    private systemes: SystemBase[] = null;
    private services: ServicesBase[] = null;

    private configTable: ConfigService = null;
    private configNewestTable: ConfigService = null;

    private levelSystem: LevelSystem = null;
    private uiSystem: UISystem = null;
    private readonly gameBundleName: string = 'gameBundle';
    private readonly frameBundleName: string = 'GameFrameBundle';

    private gotTips: boolean = false;
    private gotAnswer: boolean = false;
    public gameEnd: boolean = false;

    public curLoadBundleName: string = null
    public set GotTips(value) {
        // console.warn("改动了提示获取", value);
        this.gotTips = value;
    };
    public get GotTips() {
        return this.gotTips;
    }

    public set GotAnswer(value) {
        // console.warn("改动了答案获取", value);
        this.gotAnswer = value;
    };
    public get GotAnswer() {
        return this.gotAnswer;
    }

    public get UIRoot() {
        return this.uiRoot;
    }

    public get Data_Cont() {
        return data_control;
    }

    public get UIModel() {
        return O1HD.getModules().uiControlModule;
    }

    public GetBundle(bundle: string): cc.AssetManager.Bundle {
        // console.info("GetBundle", bundle)
        this.curLoadBundleName = bundle
        return cc.assetManager.getBundle(bundle);
    }

    public get frameBundle() {
        return cc.assetManager.getBundle(this.frameBundleName);
    }

    public get gameBundle() {
        return cc.assetManager.getBundle(this.gameBundleName);
    }

    public get ConfigSerive() { return this.configTable; }

    public get LevelSystem() { return this.levelSystem; }
    public get UISystem() { return this.uiSystem; }
    public platName: string = null;

    public async GameInit() {
        let before = Date.now();
        // console.log("---<<<<<<游戏逻辑框架初始化>>>>>>---");
        await new Promise<void>((resolve, reject) => {
            cc.assetManager.loadBundle(this.frameBundleName, (err) => {
                if (err) { console.error("逻辑框架分包加载错误", err); reject(); return; }
                // console.log("---<<<<<<游戏逻辑框架分包加载完毕>>>>>>---");
                resolve();
            });
        });

        cc.assetManager.loadBundle("normalMode", (err) => {
            if (err) {
                // console.error("normal游戏包加载错误");
                return;
            }
        })

        await this.ServiceInit();
        this.SystemInit();
        let tt = window['tt'];
        if (tt) {
            tt.getSystemInfo({
                complete: ((res) => {
                    if (res) {
                        this.platName = res.appName;
                        // console.warn("app名称", this.platName);
                    }
                })
            })
        }
        // console.log("--->>>>>>>>>>游戏逻辑框架初始化完毕,耗时:", Date.now() - before, "ms");
        return Promise.resolve();
    }
    /**是否能开启添加桌面  */
    isShortcutOpen() { //字节跳动 字节跳动lite 和西瓜拒审 关闭
        if (this.platName == "Toutiao"
            || this.platName == "XiGua"
            || this.platName == "news_article_lite"
        ) {

            return false
        }

        return true
    }

    private async ServiceInit() {
        this.configTable = new ConfigService();
        this.services = [this.configTable];
        let array = this.services;
        for (let index = 0; index < array.length; index++) {
            const element = array[index];
            await element.ServiceInit();
        }
    }

    private SystemInit() {
        this.levelSystem = new LevelSystem();
        this.uiSystem = new UISystem();
        this.systemes = [this.levelSystem, this.uiSystem];
        let array = this.systemes;
        for (let index = 0; index < array.length; index++) {
            const element = array[index];
            element.SystemInit();
        }
    }

    SetUIRoot(node: cc.Node) {
        this.uiRoot = node;
    }
    async GameEnd(win: boolean) {
        if (this.gameEnd) { return; }
        this.gameEnd = true;
        O1HD.emit(GAME_EVENT.ON_GAME_END, win);
        this.uiSystem.ClosePage(PAGE.INGAMEPAGE);
    }
}

function Gen() {
    try {
        window["mainRoot"] = new MainRoot();
        return window["mainRoot"];
    } catch (e) {
        console.error(e, "主模式引擎创建失败");
    }
}


let mainRoot: MainRoot = Gen();
export default mainRoot;